use anyhow::{Context, Result};
use libc::exit;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{fmt, prelude::__tracing_subscriber_SubscriberExt, Registry};

pub struct Logs {}  // 空结构体  仅作为日志初始化方法的容器

impl Logs {
	pub fn init() -> Result<WorkerGuard> {
        // 获取日志文件的存储路径
		let root = xdg::BaseDirectories::with_prefix("sparrow")
			.context("failed to get XDG base directories")?
			.get_state_home();
        // 创建按小时滚动的日志写入器
		let appender = tracing_appender::rolling::minutely(root, "sparrow.log");
		let (handle, guard) = tracing_appender::non_blocking(appender);

		// let filter = EnvFilter::from_default_env();
        // 配置日志订阅器
		let subscriber = Registry::default().with(fmt::layer().pretty().with_writer(handle));

        // 设置全局日志订阅器
		tracing::subscriber::set_global_default(subscriber)
			.context("setting default subscriber failed")?;
        // 返回日志守护器
		Ok(guard)
	}
}
